using System;
using System.Collections.Generic;
using System.Linq;
namespace WebClient.Controllers
{
	#region using
	using System.Web.Mvc;
	using WebClient.Models.ViewModels;
	using WebClient.Models;
	using WebClient.Models.Repositories;
	using WebClient.Helpers;
	using System.Web.Security;
    using WebClient.CF_DAL_WS;
	#endregion
	public partial class AdminController: BaseController
	{
		public ActionResult SelectContact()
		{
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_CONTACT
			};
			return View(CreateViewModel(data));
		}

		public ActionResult DeleteContact(int id, int page = 1)
		{
			var result = _repContact.Delete(id);
			var data = new DataTransferViewModel
			{
				InfoText = result ? "Item has been deleted" : "Cannot delete item!",
				EnumViewModelType = EnumViewModel.ADMIN_CONTACT,
				CurrentPage = page
			};
			return View("SelectContact", CreateViewModel(data));
		}

		public ActionResult EditContact(int page, int id)
		{
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_CONTACT,
				CurrentPage = page,
				IntID = id
			};
			return PartialView("Templates/TH_EditContact", CreateViewModel(data));
		}

		public ActionResult CancelEditingContact(int page)
		{
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_CONTACT,
				CurrentPage = page
			};
			return PartialView("Templates/TH_ListContact", CreateViewModel(data));
		}

		[ValidateInput(false)]
		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult UpdateContact(FormCollection forms)
		{
			string id = forms.GetValues("Contact_ID")[0];
			string email = forms.GetValues("Contact_Email")[0];
			string name = forms.GetValues("Contact_Name")[0];
			string subject = forms.GetValues("Contact_Subject")[0];
			string detail = forms.GetValues("Contact_Detail")[0];
			var contact = _repContact.SelectByID(int.Parse(id));
			contact.Email = email;
			contact.Name = name;
			contact.Subject = subject;
			contact.Detail = detail;
			var result = _repContact.Update(contact);
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_CONTACT,
			};
			return View("SelectContact", CreateViewModel(data));
		}


		[ValidateInput(false)]
		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult InsertContact(FormCollection forms)
		{
			string email = forms.GetValues("Contact_Email")[0];
			string name = forms.GetValues("Contact_Name")[0];
			string subject = forms.GetValues("Contact_Subject")[0];
			string detail = forms.GetValues("Contact_Detail")[0];
			var newItem = new ContactDTO
			{
				Email = email,
				Name = name,
				Subject = subject,
				Detail = detail
			};
			var result = _repContact.Insert(newItem);
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_CONTACT,
				InfoText = result ? "New item has been added" : "Cannot insert new item"
			};
			return View("SelectContact", CreateViewModel(data));
		}


	}
}